#!/usr/bin/php
<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 */

date_default_timezone_set('UTC'); 

define('CURRENT_DIR',dirname(__FILE__));
$last_slash = strrpos(dirname(__FILE__), "/");
if ($last_slash<=0) {
	$last_slash = strrpos(dirname(__FILE__), "\\");
}
define('ROOT_PATH',substr(CURRENT_DIR, 0, $last_slash)."/");

$debugenabled=false;
$maildebugenabled=false;
$dbdebugenabled=false;

error_reporting(0);
ini_set('display_errors', true);

include_once(ROOT_PATH."configs/dbconf.php");
include_once(CURRENT_DIR."/func_stats.php");

require_once(ROOT_PATH."class/dbmanager.class.php");
require_once(ROOT_PATH."class/loader.class.php");
require_once(ROOT_PATH."includes/util.func.php");

require_once(ROOT_PATH."class/user.class.php");
require_once(ROOT_PATH."includes/Tar.php");
include_once(ROOT_PATH."func/deltree.inc.php");
include_once(ROOT_PATH."class/upload.class.php");
include_once(ROOT_PATH."class/files.class.php");
include_once(ROOT_PATH."class/history.class.php");
require_once(ROOT_PATH."class/def.class.php");

$db = new dbmanager(DB_HOST,DB_USER,DB_PASS);

$db->connect(DB_NAME);
$loader = new loader($db);

/* LOAD DEFINITIONS */
$defs = simplexml_load_file(ROOT_PATH."/configs/definitions.xml");
foreach($defs->definition as $def) {
   eval("define('{$def->name}', '{$def->value}');");
}

$defs = simplexml_load_file(ROOT_PATH."/configs/tables.xml");
foreach($defs->table as $def) {
   eval("define('{$def->name}', '{$def->value}');");
}

$def = new def;
$list = $loader->loadList($def);

foreach($list as $obj) {
   eval("define('{$obj->id}', '{$obj->value}');");
}

if (ENABLE_CUSTOM_SMTP == 'true') {
	debug("Using custom SMTP");
	ini_set("smtp",SMTP_HOST);
	ini_set("smtp_port",SMTP_PORT);
}

/* END */
$upload = new upload;
$orphans = $loader->loadListBy($upload,$upload->listOrphans());

echo "Orphans ".count($orphans)."\n";

foreach ($orphans as $u) {
	$loader->delete($u);
	$filename = UPLOAD_PATH."/".$u->id;
	unlink($filename);
}

$expired = $loader->loadListBy($upload,$upload->listExpired());

echo "Expired ".count($expired)."\n";

foreach ($expired as $u) {

	foreach ($u->filesid as $fid) {
		$file = new files($u->projectid);
		$file->id=$fid;
		$ret = $loader->get($file);
		if ($ret) {
			if ($file->statusid = FILE_STATUS_CMPND) {
				$file->statusid = FILE_STATUS_LOCKED;
				$file->auto = 0;
				$file->stamp=util::hashcode($file->id);
				$loader->update($file);

				$his = new history($u->projectid);
				$his->statusid=$file->statusid;
				$his->fileid=$file->id;
				$his->userid=BOT_USER;
				$his->comment="Upload rejected. Older than ".UPLOAD_EXPIRE." days";
				$loader->save($his);
			}
		}
	}
	
	$loader->delete($u);
	$filename = UPLOAD_PATH."/".$u->id;
	unlink($filename);
}

echo "Clean TEMP\n";

deltreeatime(TEMP_PATH);

echo "Done.\n";

$db->close();

?>